package com.carvajal.adminpos.views.activities.purchase;

import java.util.ArrayList;
import java.util.List;

import com.carvajal.adminpos.R;
import com.carvajal.adminpos.controllers.purchase.PurchaseListPurchaseController;
import com.carvajal.adminpos.helpers.AdminPOSDbHelper;
import com.carvajal.adminpos.models.Commerce;
import com.carvajal.adminpos.models.Purchase;
import com.carvajal.adminpos.models.actions.PurchaseAction;
import com.carvajal.adminpos.views.adapters.PurchaseListAdapter;

import android.R.menu;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;

public class PurchaseListActivity extends Activity {
	
	private ListView listPurchase;
	PurchaseListPurchaseController purchaseListController;
	SearchView mSearchView;
	MenuItem searchItem;
	PurchaseListAdapter adaptador;
	
	AdminPOSDbHelper usdbh;
	SQLiteDatabase db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_purchase_list_purchase);
		
		adaptador = new PurchaseListAdapter(this,new ArrayList<Purchase>());
				 
		listPurchase = (ListView)findViewById(R.id.ListaCompras);
		 
		listPurchase.setAdapter(adaptador);
		
		purchaseListController = new PurchaseListPurchaseController(this);

	}
	
	@Override
	protected void onStart() {
		List<Purchase> data = purchaseListController.consultarListaCompra();
		actualizarListaCompras(data);
		super.onStart();
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.purchase_main_purchase, menu);
		MenuItem searchItem = menu.findItem(R.id.itemBuscar);

	    mSearchView = (SearchView) searchItem.getActionView();
        mSearchView.setQueryHint("Id...");
	     
        mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
			
        	@Override
        	public boolean onQueryTextChange(String newText) {
        		purchaseListController.realizaBusquedaCompra(newText);
        		return false;
        	}

        	@Override
        	public boolean onQueryTextSubmit(String text) {
        		        		
        		purchaseListController.realizaBusquedaCompra(text);
       		
        		return false;
        	} 	
    	
		});
        
		return true;
	}
	

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		/*chenao - Evalua los items existentes y genera el llamado
		 * al metodo correspondiente dependiendo de lo que presione el usuario*/		
	  switch (item.getItemId()) {
	  case R.id.itemAgregar:
		  purchaseListController.llamaFormularioRegistro();
	    break;
	  }

	  return true;
	}
	
	public void addEventItemAgregar(OnItemClickListener agregarComprasItemListener) {
		listPurchase.setOnItemClickListener(agregarComprasItemListener);
	}

	public void actualizarListaCompras(List<Purchase> lista) {
		if ( null != lista ) {
			adaptador.clear();
			adaptador.addAll(lista);
		}
	}

}
